#!/bin/bash
# DEBIAN
#

# The MIT License
#
# Copyright (c) 2009, Nick Galbreath
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in
# all copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
# THE SOFTWARE.


set -e
HERE="`pwd`"
TREEDIR="${HERE}/trees"
TARDIR="${HERE}/tars"
BUILDDIR="${HERE}/tmp"

mkdir -p ${TREEDIR}
mkdir -p ${TARDIR}
mkdir -p ${BUILDDIR}

CONF=$1
echo "Reading source file: ${CONF}"

source ${CONF}

echo "REPOSITORY  = ${REPOSITORY}"
echo "TAG         = ${TAG}"
echo "VERSION     = ${VERSION}"
echo "RELEASE     = ${RELEASE}"

DEST=${BUILDDIR}/${PKGNAME}-${VERSION}
NAME=${PKGNAME}-${VERSION}-${RELEASE}
TARBALL=${NAME}.tar.bz2

rm -rf ${DEST}
echo "BUILDDIR = ${BUILDDIR}"
echo "TARBALL  = ${TARDIR}/${TARBALL}"
tar -C ${BUILDDIR} -xjvf ${TARDIR}/${TARBALL}

mkdir -p ${DEST}/debian
cp debian/copyright ${DEST}/debian/copyright
cp debian/compat ${DEST}/debian/compat
cp debian/docs ${DEST}/debian/docs

sed -e "s/_NAME_/${NAME}/g;s/_PKGNAME_/${PKGNAME}/g;s/_VERSION_/${VERSION}/g;s/_RELEASE_/${RELEASE}/g;" < debian/control > ${DEST}/debian/control
sed -e "s/_NAME_/${NAME}/g;s/_PKGNAME_/${PKGNAME}/g;s/_VERSION_/${VERSION}/g;s/_RELEASE_/${RELEASE}/g;" < debian/changelog > ${DEST}/debian/changelog
sed -e "s/_NAME_/${NAME}/g;s/_PKGNAME_/${PKGNAME}/g;s/_VERSION_/${VERSION}/g;s/_RELEASE_/${RELEASE}/g;" < debian/rules > ${DEST}/debian/rules
chmod a+x ${DEST}/debian/rules


# can't set these to "nothing"..# prevent dpkg using "-O2 -g"
# mozilla sets the correct opt flags which in this case is -O3
#
export CFLAGS=-O3
export CXXFLAGS=-O3

(cd ${DEST}; dpkg-buildpackage -b -rfakeroot)

